<div id="loading">(Please wait while dojo json-rpc client loads...)</div>
<script type="text/javascript" src="dojo_rpc_only.js"></script>
<script type="text/javascript">
	// create json-rpc service
	hostDef = {"serviceType": "JSON-RPC", "serviceURL": "ace_json_server.cgi", "sync": true}
	host = new dojo.rpc.JsonService({smdObj:hostDef});

	// Avaible Processes (update):
	function doUpdateAvailable() {var myDeferred = host.callRemote("get_names_of_available_workflows", []).addCallback(onUpdateAvailableDone);}
	function onUpdateAvailableDone(result) {
		document.getElementById("available_processes_table").innerHTML = '<tr><th>Workflow</th><th>Status</th><th>Details</th><th>Actions</th></tr>'
		for (var result_index in result) {
			var td_tags = "";
            td_tags += '<td>'+result[result_index]['workflow_name']+'</td>';
            td_tags += '<td>'+result[result_index]['status']+'</td>';
            td_tags += '<td>'+'<a href>click</a>'+'</td>';
            td_tags += '<td>'+'<a style="{text-decoration:underline; color:blue;}" onclick="doRestartWorkflow(\''+result[result_index]['workflow_name']+'\');">start</a>'+'</td>';
		    document.getElementById("available_processes_table").innerHTML += '<tr>' + td_tags + '</tr>';
		}
	}
	
	// Running Processes (update):
	function doUpdateRunning() {var myDeferred = host.callRemote("get_names_of_running_workflows", []).addCallback(onUpdateRunningDone);}
	function onUpdateRunningDone(result) {
		document.getElementById("running_table2").innerHTML = '<tr><th>Workflow</th><th>Status</th><th>Input</th><th>Details</th><th>Actions</th></tr>'
		for (var result_index in result) {
			var td_tags = "";
            td_tags += '<td>'+result[result_index]['workflow_name']+'</td>';
            td_tags += '<td>'+result[result_index]['status']+'</td>';
            td_tags += '<td>'+result[result_index]['input']+'</td>';
            //td_tags += '<td>'+'<input type="text" id="input_text" name="input_text" size="3"></td>'+'</td>';
            td_tags += '<td>'+'<a href>click</a>'+'</td>';
            td_tags += '<td>'+'<a style="{text-decoration:underline; color:blue;}" onclick="doFdStep(\''+result[result_index]['workflow_name']+'\');">fw_step</a>'+'</td>';
		    document.getElementById("running_table2").innerHTML += '<tr>' + td_tags + '</tr>';
		}
	}
	
	function doFdStep(workflow_name) {
	   //var input_text = document.getElementById("input_text").value;	   
	   var form_values = [];       var form = document.getElementById("input_form");       for ( var i = 0; i < form.elements.length; i++ ) {          form_values.push(form.elements[i].value);       }
	   var myDeferred = host.callRemote("fd_step", [workflow_name,form_values]).addCallback(onFdStepDone);
	}
	function onFdStepDone(result) {doUpdateRunning();}
	
	function doRestartWorkflow(workflow_name) {var myDeferred = host.callRemote("restart_workflow", [workflow_name]).addCallback(onRestartWorkflowDone);}
	function onRestartWorkflowDone(result) {doUpdateRunning(); doUpdateCompleted();}

	function doReactivateCompletedWorkflow(workflow_namedate_key, step_num) {var myDeferred = host.callRemote("reactivate_completed_workflow", [workflow_namedate_key, step_num]).addCallback(onReactivateCompletedWorkflowDone);}
	function onReactivateCompletedWorkflowDone(result) {doUpdateRunning(); doUpdateCompleted();}

	
	// Completed Processes (update):
    function doUpdateCompleted() {var myDeferred = host.callRemote("get_list_of_completed_workflows", []).addCallback(onUpdateCompletedDone);}
	function onUpdateCompletedDone(result) {
		document.getElementById("completed_table").innerHTML = '<tr><th>Workflow</th><th>Status</th><th>Details</th><th>Actions</th><th>Restart at step</th></tr>'
		for (var result_index in result) {
			var td_tags = "";
            td_tags += '<td>'+result[result_index]['workflow_name']+'</td>';
            td_tags += '<td>'+result[result_index]['status']+'</td>';
            td_tags += '<td>'+'<a href>click</a>'+'</td>';
            td_tags += '<td>'+'<a style="{text-decoration:underline; color:blue;}" onclick="doFdStep(\''+result[result_index]['workflow_name']+'\');">fw_step</a>'+'</td>';
		    // create clickable: step 0, 1, 2, ... step_num
		    var links_string = "";
		    for (var step_num=0; step_num<result[result_index]['step_num']; step_num++) {
		        var workflow_namedate_key = result[result_index]['workflow_name'];
		    	links_string += ' <a style="{text-decoration:underline; color:blue;}" onclick="doReactivateCompletedWorkflow(\''+workflow_namedate_key+'\', '+step_num+');">'+step_num+'</a> ';
		    }
		    td_tags += '<td>'+links_string+'</td>';
		    document.getElementById("completed_table").innerHTML += '<tr>' + td_tags + '</tr>';
		}
	}

	

	dojo.byId("loading").innerHTML = "";
</script>



<H1>Top-Level Dashboard: All Workflows</H1>

<p>Avaible Processes (<a style="{text-decoration:underline; color:blue;}" onclick="doUpdateAvailable();">update</a>):<br>
<table border=4 id="available_processes_table"></table>

<p>Running Processes (<a style="{text-decoration:underline; color:blue;}" onclick="doUpdateRunning();">update</a>):<br>
<table border=4 id="running_table2"></table>

<p>Completed Processes (<a style="{text-decoration:underline; color:blue;}" onclick="doUpdateCompleted();">update</a>):<br>
<table border=4 id="completed_table"></table>
